﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iOak.Business.Campaigns;
using iOak.Business.Resumes;
using iOak.Domain.Campaigns;
using iOak.Common;
using iOak.Core;
using iOak.Domain.Resumes;

namespace iOak.Web.ExeclHelper
{
    public static class ExeclReflectionHelper
    {
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly ApplicationCandidateService itemApplicationCandidateServic = new ApplicationCandidateService();
        private static readonly CampaignService itemCampaignService = new CampaignService();
        private static readonly CompanyService itemCompanyService = new CompanyService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        private static readonly ResumeService itemResumeService = new ResumeService();
        private static readonly PolicyService itemPolicyService = new PolicyService();
        private static readonly ResumeTemplateFieldsService itemResumeTemplateFieldsService = new ResumeTemplateFieldsService();
        public static Dictionary<string, Company> CompanyList = new Dictionary<string, Company>();

        public static Company GetCompany(ApplicationCandidate item)
        {
            var itemCompany = new Company();

            if (CompanyList.Keys.Contains(item.ApplicationId.ToString()))
            {
                itemCompany = CompanyList[item.ApplicationId.ToString()];
            }
            else
            {
                var itemApp = itemApplicationService.Retrieve(item.ApplicationId);
                itemCompany = itemCompanyService.Retrieve(itemApp.CompanyId);
                CompanyList.Add(item.ApplicationId.ToString(), itemCompany);
            }

            return itemCompany;
        }

        public static string GetCompanyName(ApplicationCandidate item, Campaign itemCampaign)
        {
            string companyName = string.Empty;
            if (CompanyList.Keys.Contains(item.ApplicationId.ToString()))
            {
                companyName = CompanyList[item.ApplicationId.ToString()].CompanyName;
            }
            else
            {
                var itemApp = itemApplicationService.Retrieve(item.ApplicationId);
                var itemCompany = itemCompanyService.Retrieve(itemApp.CompanyId);
                companyName = itemCompany.CompanyName;
                CompanyList.Add(item.ApplicationId.ToString(), itemCompany);
            }
            return companyName;
        }

        public static string GetPracticeTime(string startTime, string endTime, ApplicationCandidate item, Campaign itemCampaign)
        {
            string returnTime = string.Empty;
            DateTime dtStart = Convert.ToDateTime(startTime);
            DateTime dtEnd = Convert.ToDateTime(endTime);
            if (dtStart.CompareTo(dtEnd) > 0)
            {
                returnTime = dtEnd.ToString("yyyy.MM.dd") + "-" + dtStart.ToString("yyyy.MM.dd");
            }
            else
            {
                returnTime = dtStart.ToString("yyyy.MM.dd") + "-" + dtEnd.ToString("yyyy.MM.dd");
            }
            return returnTime;
        }

        public static string GetSuggestion( ApplicationCandidate item, Campaign itemCampaign)
        {
            return ResourceHelper.GetCandidateStatusName(item.Passed);
        }

        public static string GetMonth(string startTime, string endTime, ApplicationCandidate item, Campaign itemCampaign)
        {
            if (item.Passed.HasValue && item.Passed.Value)
            {
                int month = CommonHelper.GetMonth(Convert.ToDateTime(startTime), Convert.ToDateTime(endTime));
                if (month == 0)
                {
                    month = 1;
                }
                return month.ToString();
            }
            else
            {
                return string.Empty;
            }
        }

        public static string GetComments(ApplicationCandidate item, Campaign itemCampaign)
        {
            return item.Notes;
        }

        public static string GetSubsidyAmount(string startTime, string endTime, ApplicationCandidate item, Campaign itemCampaign)
        {
            if (item.Passed.HasValue && item.Passed.Value)
            {
                int month = CommonHelper.GetMonth(Convert.ToDateTime(startTime), Convert.ToDateTime(endTime));
                if (month == 0)
                {
                    month = 1;
                }
                return Math.Round(month * itemCampaign.Bounty, 2).ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}